import qs from 'qs'
export default {
    name: 'Runningreport',
    components: {

    },
    data() {
        return {
            height:'600',
            total:0,
            pagesize:5,
            currentPage:1,
            runningData:[],
            timeInterval: [this.NowTimestamp((new Date()).getTime() - 4665600000), this.NowTimestamp((new Date()).getTime())],
            multipleSelection:[]
        }
    },
    computed: {
        offset:function(){
            return(this.currentPage -1) * this.pagesize;
        }
    },
    watch: {

    },
    created() {
        this.initRuninList();
    },
    mounted() {

    },
    methods: {
        NowTimestamp(timestamp) {
            var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
            var Y = date.getFullYear() + '-';
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
            var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + ' ');
            var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
            var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
            var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
            return Y + M + D;
        },
        //运行报表查询
        initRuninList() {
          var listParamsObj = {
            limit:this.pagesize,
            offset:this.offset,
          }
          this.$http.post('/deviceCommonInfoController/devicesPage',qs.stringify(listParamsObj))
           .then((res) => {
             if(res.status == 200){
                 this.runningData = res.data;
                 this.total = res.total;
             } else {
                this.runningData = [];
                this.total = 0;
             }
            }).catch((err) => {
            console.log(err);
          });
        },
        //导出excel表格
        exportClick() {
            //导出修正文件名--利用a标签
            if (this.multipleSelection.length < 1) {
                this.$message.error("请选择要导出的异常信息");
                return false;
            }else{
                var rowKeys = [];
                this.multipleSelection.forEach(function(val,idx){
                    rowKeys.push(val.rowKey);
                });
                var txtName = rowKeys.join(",");
                this.$http.post('/deviceCommonInfoController/deviceExportRecordExport', this.qs.stringify({
                    rowKeys:txtName
                }), {
                    responseType: 'arraybuffer'
                }).then((res) => {
                        const content = res;
                        const blob = new Blob([content], {
                        type: "application/vnd.ms-excel"
                        })
                        const fileName = "运行报表.xls";
                        if('download' in document.createElement('a')){
                           //非IE下载
                           const elink = document.createElement("a");
                           elink.download = fileName;
                           elink.style.display = "none";
                           elink.href = URL.createObjectURL(blob);
                           document.body.appendChild(elink);
                           elink.click();
                           URL.revokeObjectURL(elink.href);//释放URL对象
                           document.body.removeChild(elink);
                        } else {
                            //IE10下载
                            navigator.msSaveBlob(blob,fileName);
                     }
                 }).catch((err) => {
                  console.log(err);
              });
            }
        },
        toggleSelection(rows) {
            if (rows) {
                rows.forEach(row => {
                    this.$refs.multipleTable.toggleSelection(row);
                });
            } else {
                this.$refs.multipleTable.clearSelection();
            }
        },
        handleSelectionChange(val){
           this.multipleSelection = val;
        },
        handleSizeChange(val){
            this.pagesize  = val;
            this.initRuninList();
        },
        handleCurrentChange(val){
            this.currentPage = val;
            this.initRuninList();
        }
    },
}